<div class="panel">

    <form class="layui-form" action="">
        <div class="layui-form-item">
            <label class="layui-form-label">比例:</label>
            <div class="layui-input-block">
                <input style="color: red;width: 100px;border: 0" type="text" name="bill_scale" lay-verify="title" class="layui-input" disabled>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">类型:</label>
            <div class="layui-input-block">
                <button name="btn_gold" type="button" class="layui-btn">金币</button>
                <button name="btn_rmb" type="button" class="layui-btn layui-btn-primary">RMB</button>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">买卖:</label>
            <div class="layui-input-block">
                <input type="radio" lay-filter="paymode" name="paymode" value="收" title="收" checked="checked">
                <input type="radio" lay-filter="paymode" name="paymode" value="出" title="出">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">方式:</label>
            <div class="layui-input-block">
                <input type="radio" lay-filter="TSTM" name="TSTM" value="直接交易" title="直接交易" checked="">
                <input type="radio" lay-filter="TSTM" name="TSTM" value="邮箱交易" title="邮箱交易">
                <input type="radio" lay-filter="TSTM" name="TSTM" value="拍卖行" title="拍卖行">
            </div>
        </div>
        <div class="layui-form-item ProceduresPanel">
            <label class="layui-form-label">手续</label>
            <div class="layui-input-block">
                <input type="checkbox" lay-filter="Procedures" name="Procedures" lay-skin="switch" lay-text="全包|不包" >
            </div>
        </div>
        <div class="changeMode">
            <div class="layui-form-item">
                <label class="layui-form-label">金币:</label>
                <div class="layui-input-block">
                    <input type="text" onkeyup="SetSum()" name="bill_gold" lay-verify="title" autocomplete="off" placeholder="请输入金币量" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">金额:</label>
                <div class="layui-input-block">
                    <input style="color: red;width: 100px;border: 0" type="text" name="bill_sum" lay-verify="title" class="layui-input" disabled>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label"></label>
            <div class="layui-input-block">
                <input style="color: green;border: 0" type="text" name="imitateresult" lay-verify="title" class="layui-input" disabled>
            </div>
        </div>
    </form>
</div>

<script>
    $(function () {
        $.getJSON ("getConfig", function (result) {
            var data=result["data"][0];
            $("input[name='bill_scale']").val("收" + data["scale_sell"] + "  :  出" + data["scale_pay"]);
        });
    });
    layui.use('form', function(){
        var form = layui.form;
        form.render();
        form.on('switch(Procedures)', function(obj) {
            var stauts=obj.elem.checked;
            if(stauts){
                $("input[name='Procedures']").attr("checked",true);
            }else {
                $("input[name='Procedures']").attr("checked",false);
            }
            SetSum();
        });
    });
    var paymode = "收" , TSTM = "直接交易" , imitate="Gold";
    layui.use(['form', 'jquery'], function() {
        var form = layui.form
        form.on('radio(paymode)', function (data) {
            paymode = data.value;
            SetSum();
        });
        form.on('radio(TSTM)', function (data) {
            TSTM = data.value;
            SetSum();
        });
    });
    function SetSum(){
        if(imitate == "Gold"){
            SetRMB();
        }else if(imitate == "RMB"){
            SetGold();
        }
    }
    function SetRMB(){
        $.getJSON ("getConfig", function (result) {
            var data=result["data"][0];
            var Procedures = $("input[name='Procedures']").attr("checked")
                ,gold = $("input[name='bill_gold']").val() * 10000
                ,shouxu = 0.97;
            if(TSTM == "邮箱交易"){
                shouxu = 0.95;
            }
            if (Procedures == "checked") {
                shouxu = 1 ;
            }
            if(paymode == "收") {
                var result = Number(gold) / Number(data["scale_sell"] * 10000) * shouxu;
                $("input[name='bill_sum']").val(result.toFixed(2));
                $("input[name='imitateresult']").val(paymode + $("input[name='bill_gold']").val() / 10000 + "万游戏币" + " = " + result + "元");
            }else {
                shouxu = 1;
                if (Procedures == "checked") {
                    shouxu = 0.97 ;
                }
                var result = Number(gold) / Number(data["scale_pay"] * 10000) * shouxu;
                $("input[name='bill_sum']").val(result.toFixed(2));
                $("input[name='imitateresult']").val(paymode + $("input[name='bill_gold']").val() / 10000 + "万游戏币" + " = " + result + "元");
            }
        });
    }

    function SetGold(){
        $.getJSON ("getConfig", function (Configresult) {
            var data=Configresult["data"][0];
            var Procedures = $("input[name='Procedures']").attr("checked")
                ,RMB = $("input[name='bill_rmb']").val()
                ,shouxu = 0.97
                ,scales = data ["scale_sell"];
            if(TSTM == "邮箱交易"){
                shouxu = 0.95;
            }
            if (Procedures == "checked") {
                shouxu = 1 ;
            }
            if(paymode == "出") {
                scales = data["scale_pay"];
                shouxu = 1 ;
                if (Procedures == "checked") {
                    shouxu = 0.97 ;
                }
            }

            var result = Number(RMB) * (Number(scales * 10000) / shouxu);
            $("input[name='rmb_bill_gold']").val(result);
            $("input[name='imitateresult']").val(paymode  + RMB + "元" + " = " + (result / 10000).toFixed(2) + "万游戏币");
        });
    }
    
    $("button[name='btn_gold']").click(function () {
        $(this).removeClass("layui-btn-primary");
        $("button[name='btn_rmb']").addClass("layui-btn-primary");
        $.ajax({
            type:"get"
            ,async: false
            ,url:"Popup/PayImiteGold.html"
            ,success:function(data) {
                $(".changeMode").html(data);
                imitate = "Gold";
                $("input[name='imitateresult']").val("");
            }
        });


    });
    $("button[name='btn_rmb']").click(function () {
        $(this).removeClass("layui-btn-primary");
        $("button[name='btn_gold']").addClass("layui-btn-primary");
        $.ajax({
            type:"get"
            ,async: false
            ,url:"Popup/PayImiteRMB.html"
            ,success:function(data) {
                $(".changeMode").html(data);
                imitate = "RMB";
                $("input[name='imitateresult']").val("");
            }
        });
    });
</script>